package com.xxkj.drugtraceability.ui.login

import android.text.InputType
import android.util.Base64
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.base.BaseVMActivity
import com.xxkj.drugtraceability.bean.LoginRequest
import com.xxkj.drugtraceability.databinding.ActivityLoginBinding
import com.xxkj.drugtraceability.dialog.BlueDialog
import com.xxkj.drugtraceability.ext.clickN
import com.xxkj.drugtraceability.ext.showLog
import com.xxkj.drugtraceability.utils.CommonUtils.getDeviceUUid
import com.xxkj.drugtraceability.utils.toast
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * author: zhoujingjin
 * created on: 2024/9/9 16:05
 * description:
 */
class LoginActivity:BaseVMActivity() {
    private val binding by binding<ActivityLoginBinding>(R.layout.activity_login)
    private val viewModel by viewModel<LoginViewModel>()

    override fun initView() {
        binding.apply {
            ivCode.clickN {
                //刷新code
                viewModel.getCode()
            }
            //点击眼睛
            ivPassword.setOnClickListener {
                viewModel.changePass()
            }
            btnLogin.clickN {
                if (etAccount.text.isNullOrEmpty()){
                    toast(R.string.need_account)
                    return@clickN
                }
                if (etPassword.text.isNullOrEmpty()){
                    toast(R.string.need_password)
                    return@clickN
                }
//                if (etCode.text.isNullOrEmpty()){
//                    toast(R.string.need_code)
//                    return@clickN
//                }
                viewModel.login(LoginRequest(etAccount.text.toString(),etPassword.text.toString(),etCode.text.toString(),viewModel.uuid),this@LoginActivity)
            }
        }
    }

    override fun initData() {
        viewModel.getCode()
    }

    override fun startObserve() {
        viewModel.imageBase64.observe(this, Observer{
            it?.let { url->
                Glide.with(this).load(Base64.decode(url, Base64.NO_WRAP)).into(binding.ivCode)
            }
        })
        viewModel.eyeType.observe(this, Observer {
            if (it){
                binding.ivPassword.setImageResource(R.mipmap.icon_eye_open)
                binding.etPassword.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
            }
            else{
                binding.ivPassword.setImageResource(R.mipmap.icon_eye_close)
                binding.etPassword.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            }
        })
        viewModel.uiState.observe(this, Observer {
            if (it.showLoading){
                showMyDialog(resources.getString(R.string.is_logining))
            }else{
                dismissMyDialog()
            }
            it.showError?.let {error->
                BlueDialog(this,error,type = 1,action = {})
            }
        })
    }
}